package tms.persistence.time;

import tms.model.login.UserEntity;
import tms.model.time.TimeEntity;
import tms.persistence.ProviderBase;
import tms.util.helper.DateHelper;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.Query;
import java.util.Date;
import java.util.List;


@Local
@Stateless
public class TimeProvider extends ProviderBase {

    @SuppressWarnings("unchecked")
    public List<TimeEntity> getTimesForPeriod(Date firstDate, Date secondDate, Integer userId) {
        Query query = getEntityManager().createNamedQuery("TimeEntity.getForPeriod");
        query.setParameter("firstDate", firstDate);
        query.setParameter("secondDate", secondDate);
        query.setParameter("userId", userId);
        return query.getResultList();
    }

    public void updateRunningTime(long triggerPeriod) {
        Query query = getEntityManager().createNamedQuery("TimeEntity.updateRunning");
        query.setParameter("triggerPeriod", triggerPeriod);
        query.executeUpdate();
    }

    public void createTime(TimeEntity timeEntity) {
        getEntityManager().persist(timeEntity);
    }

    public void stopRunningTimes(Date stopTime, Integer userId) {
        Query query = getEntityManager().createNamedQuery("TimeEntity.stopRunningForUser");
        query.setParameter("stopDate", stopTime);
        query.setParameter("userId", userId);
        query.executeUpdate();
    }

    public void pauseTime(Integer userId) {
        Query query = getEntityManager().createNamedQuery("TimeEntity.pauseForUser");
        query.setParameter("userId", userId);
        query.executeUpdate();
    }

    public TimeEntity updateTime(TimeEntity timeEntity) {
        return getEntityManager().merge(timeEntity);
    }

    public Long getTimesNumberBeforeDate(Integer userId, Date date) {
        Query query = getEntityManager().createNamedQuery("TimeEntity.timesNumberBeforeDate");
        query.setParameter("date", date);
        query.setParameter("userId", userId);
        return (Long)query.getSingleResult();
    }

    public Long getTimesNumberAfterDate(Integer userId, Date date) {
        Query query = getEntityManager().createNamedQuery("TimeEntity.timesNumberAfterDate");
        query.setParameter("date", date);
        query.setParameter("userId", userId);
        return (Long)query.getSingleResult();
    }

    public Date getLastStartDate(Integer userId) {
        Query query = getEntityManager().createNamedQuery("TimeEntity.getLastStartDate");
        query.setParameter("userId", userId);
        return (Date)query.getSingleResult();
    }

    @SuppressWarnings("unchecked")
    public List<UserEntity> getUnnotifiedUsersWithWorkDayEnded(Date day) {
        return getEntityManager().
                createNamedQuery("UserEntity.getUnnotifiedWithWorkDayEnded").
                setParameter("dayBegin", DateHelper.getDayBegin(day)).
                setParameter("dayEnd", DateHelper.getDayEnd(day)).
                getResultList();
    }

    @SuppressWarnings("unchecked")
    public List<UserEntity> getUsersWithWorkDayEnded(Date day) {
        return getEntityManager().
                createNamedQuery("UserEntity.getWithWorkDayEnded").
                setParameter("dayBegin", DateHelper.getDayBegin(day)).
                setParameter("dayEnd", DateHelper.getDayEnd(day)).                
                getResultList();
    }
}
